/**
 * 		Main server class. Allows connection to a SQLite or MySQL database
 * 		Implements and abstracts any database functions (INSERT, UPDATE, DELETE ...)
 * 
 * 		Author:		Michael Piskurc
 * 		Date: 		December 2009
 * 		Version: 	0.1
 */
package com.tasks.server;

import java.sql.*;
import java.text.SimpleDateFormat;
import org.apache.log4j.*;

public class Server {

	public static final int 				SQLITE_DB = 0;
	public static final int 				MYSQL_DB = 1;
	public static final String 				SQLITE_DRIVERCLASS = "org.sqlite.JDBC";
	public static final String 				MYSQL_DRIVERCLASS = "com.mysql.jdbc.Driver";	
	public static final SimpleDateFormat 	df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	private int dbType;
	private String dbLocation;
	private String dbName;	
	private String username;
	private String password;
	private Settings settings;	
	private Connection connection;
	
	static Logger logger = Logger.getLogger(Server.class);
	
	public Server(Settings s)
	{
		PropertyConfigurator.configure("../properties/log4j.properties");
		settings = s;
		
		dbLocation = s.getDBlocation();
		dbName = s.getDBname();
		dbType = s.getDBType();
		username = s.getDBusername();
		password = s.getDBpassword();
		
		getConnection();
	}
		
	public void getConnection() 
	{
		String connectionString = null;
		
		if(dbLocation != null && dbName != null && dbLocation.length() > 0 && dbName.length() > 0) 
		{
			try 
			{
				if(dbType == SQLITE_DB)
				{
					Class.forName(SQLITE_DRIVERCLASS);
					connectionString = "jdbc:sqlite:" + dbLocation + dbName;	
					connection = DriverManager.getConnection(connectionString);
				}
				else if(dbType == MYSQL_DB)
				{
					Class.forName(MYSQL_DRIVERCLASS);
					connectionString = "jdbc:mysql:" + dbLocation + dbName;
					connection = DriverManager.getConnection(connectionString, username, password);
				}
				logger.info("Database connection started");
			}
			catch(Exception e)	
			{
				new ServerException(this, "getConnection", e);
			} 
		}
		else 
		{
			new ServerException("Please supply a database name and location.");
		}
	}
	
	public void close() 
	{
		if (connection != null) 
		{
              try 
              {
            	  connection.close();
            	  logger.info("Database connection terminated");
              }
              catch (Exception e) 
              { 
            	  new ServerException(this, "Error occured closing the connection", e);
              }
          }
		else 
		{
			new ServerException("No open database connections");
		}
	}
			
	public Statement getStatement()
	{
		try
		{
			return connection.createStatement();
			//return connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		}
		catch (Exception e)
		{
			new ServerException(this, "getStatement", e);
		}
		return null;
	}
	
	public DatabaseMetaData getDBMetaData()
	{
		try 
		{
			return connection.getMetaData();	
		}
		catch (Exception e)
		{
			new ServerException(e);
		}
		return null;
	}
	
	public int getDBType()
	{
		return dbType;
	}
	
	public Settings getSettings()
	{
		return settings;
	}
}
